/**
 * 新闻管理
 * **/
define(['apps/utils/tools','apps/manage'],function (tools,manage){
    function init (param) {
    	//分页点击事件
	    var options = param.options;
	    options.onPageClicked = function(e,originalEvent,type,page){
			var bean = param.obj,
			url = tools.getBasePathUrl() + bean.scode+"/query.cnbi";
			bean.currentPage = page;			
			tools.ajax({url : url,dataType : 'html',data :bean,success :success});
		};
	    $('#paginator').bootstrapPaginator(options);
		$('.pointer-cursor').css({'cursor':'pointer'});
	    bindEvent(param["obj"]);//绑定事件
    }
    
    //绑定事件
    function bindEvent(bean){
    	selectButton ();//全选和反选按钮
	    selectNews (bean);//查询新闻列表
	    clearNews (bean);//清空查询条件
	    
	    addNews (bean);//添加新闻方法
	    newsDelWin ();//编辑新闻编辑窗口
	    deleteNews ();//删除新闻方法
	    newsEditWin ();//编辑新闻编辑窗口
	    updateEditNews ();//修改新闻
	    newsHover ();//悬浮事件
    };
    
    //确认删除后、点击按钮发送删除请求事件
    function deleteNews(){
    	$("#okDelNews").click(function(){
    		var ids = $("#newsDelId").val(),url = tools.getBasePathUrl()+'/newsM/del.cnbi',
    		data = {"ids":ids,"level":Utils.paramObj.obj["level"],"scode":Utils.paramObj.obj["scode"],"spcode":Utils.paramObj.obj["spcode"],"sname":Utils.paramObj.obj["sname"]};
		    tools.ajax({url :url,dataType :'html',data :data,success :success});
		});
    }
    
    //点击或选中一行数据时触发悬浮事件
    function newsHover(){
    	$("tbody >tr").bind("click",function(){
            tools.applyTableSingleSelectClickStyle(this);//表格单行选择时调用
         });
    };
   
    //修改新闻完成后，点击保存后出发请求事件
    function updateEditNews() {
		$("#saveEditNews").click(function(){
		    newsTitle = $("#newsEditName")[0].value,newsContent = $("#newsEditContent").val(),newsId = $("#newsEditId").val();
		    if(newsTitle!=$("#title"+newsId).text()||newsContent!=$("#content"+newsId).text()){
			    $("#newsAlert").hide("500");
			    $("#saveEditNews").attr("data-dismiss","modal");
			    var url = tools.getBasePathUrl()+'/newsM/edit.cnbi',data = {"id":newsId,"title":newsTitle,"content":newsContent,"level":Utils.paramObj.obj["level"],"scode":Utils.paramObj.obj["scode"],"spcode":Utils.paramObj.obj["spcode"],"sname":Utils.paramObj.obj["sname"]};
			    tools.ajax({url :url,dataType :'html',data :data,success :success});
		    }else{
			    $("#saveEditNews").attr("data-dismiss","");
			    $("#newsAlert").show("500");
		    }
	    });
    }
    
    //增、删、改、查操作成功执行后，重新加载列表的方法
    function success(data){
    	 $("#content").empty().append(data);
			/* options 分页 对象参数 */
		 init(Utils.paramObj);
	};
	
	//按条件查询新闻列表操作
    function selectNews(bean) {
    	$("#titleSearch").click(function(){
    		var newsTitle = $("input[name='newsTitle']")[0].value.replace(/\s+/g,'');
    		if(newsTitle.length != 0){
    			bean.title = newsTitle;
    			queryNewsAjax(bean);
    		}
    	});
    	$("#timeSearch").click(function(){
    		var updateTime = $("#dtp_input2")[0].value;
    		if(updateTime.length != 0){
    			bean.updateTime = updateTime;
    			queryNewsAjax(bean);
    		}
    	});
    }
    
    //清除查询表单操作
    function clearNews(bean) {
    	$("#doClear").click(function(){
    		$("#newsTitle")[0].value = '';
    		$("#dtp_input2")[0].value = '';
    		bean.title = null;
    		bean.updateTime = null;
    		queryNewsAjax(bean);
    	});
    }
    //查询 请求方法
    function queryNewsAjax(bean){
    	var url = tools.getBasePathUrl()+'/newsM/query.cnbi';
    	tools.ajax({url : url,dataType : 'html',data :bean,success :success}); 
    };
    
    //添加新闻操作
    function addNews(bean) {
    	$("#saveAddNews").click(function(){
    		var url = tools.getBasePathUrl() + bean.scode +'/add.cnbi';
    		//添加和修改调用相同的方法
    		operatorNew('#newsForm',url,bean,'#saveAddNews');
    	});
    }
    
    /**
     * @param formId 表单form dom
     * @param url 请求地址
     * @param bean 当前页面对象
     * @param buttonId 按钮 dom
     * **/
    function operatorNew(formId,url,bean,buttonId){
		  var title = $(formId+" input[name=title]")[0].value.replace(/\s+/g,'');
		  var content = $(formId+" textarea[name=content]")[0].value.replace(/\s+/g,'');
		    // 2.最后要调用 valid()方法
		  var tooltips = '<span class="help-block" id="valierr" style="color: #a94442">请输入内容</span>';
		  if (title.length == 0){
		  	  $(formId+" input[name=title]").parent().after(tooltips);
		  	  $(buttonId).removeAttr('data-dismiss');
		      return false;
		  }else if(content.length == 0){
		  	  $(formId+" textarea[name=content]").parent().after(tooltips);
		  	  $(buttonId).removeAttr('data-dismiss');
		  }else{
		  	$(buttonId).attr({'data-dismiss':'modal'});
		  	$(formId+" input[name=title]").parent().next().empty();
		  	$(formId+" textarea[name=content]").parent().next().empty();
		  	   bean.title = title;
		  	   bean.content = content;
			  tools.ajax({url : url,dataType : 'html',data : bean,success :success});
			  var text = '';
	 	  	  if(buttonId === '#saveAddNews'){
	 	  	 		text = '添加';
	 	  		}else text = '修改';
			  	$.Zebra_Dialog(text+'新闻<code>'+title+'</code>操作完成',
			  		{'auto_close':1000,'type':false,'buttons':false});
		}
    };
    
    //弹出删除新闻框操作
    function newsDelWin() {
    	$("#doDelete").click(function(){
    		var id = 0,i = 0,ids = "";
    		$('input[type=checkbox]').each(function (){                   
		    	if(this.checked){
		    		ids += "'"+$(this)[0].value+"',";
		    		i++;
		    	}
			});
    		
    		if(ids.indexOf(",'on'")!=-1){//消除掉非法字符
    			ids = ids.replaceAll(",'on'", "");
    		}
    		
    		ids = ids.substring(0, ids.lastIndexOf(','));
    		if(i>=1){
    			$("#doDelete").attr("data-target","#delNewsModal");
    			$("#newsDelId").val(ids);
    		}else{
    			$("#doDelete").attr("data-target","");
    			//alert("提示：只少选中一条新闻进行删除操作！！！");
    			$.Zebra_Dialog('还未选择要删除的新闻...',
			  		{'auto_close':1000,'type':false,'buttons':false});
    		}
    	});
    }
    
    //弹出新闻编辑框操作
    function newsEditWin(){
    	$("#doEdit").click(function(){
    		var idArr = 0,i = 0,newsTitle = "",newsContent = "";
    		$("#newsAlert").hide();
    		$('input[type=checkbox]').each(function (){                   
		    	if(this.checked){
		    		idArr = $(this)[0].value;
		    		i++;
		    	}
			});
    		if(i==1){
    			$("#doEdit").attr("data-target","#editNewsModal");
    			$("#newsEditName").val($("#title"+idArr).text());
    			$("#newsEditContent").val($("#content"+idArr).text());
    			$("#newsEditId").val(idArr);
    		}else{
    			$("#saveEditNews").attr("data-dismiss","modal");
    			$("#doEdit").attr("data-target","");
    			$.Zebra_Dialog('还未选择要修改的新闻...',
			  		{'auto_close':1500,'type':false,'buttons':false});
    		}
    	});
    }
    
    //checkbox全选与反选操作
    function selectButton(){
    	$('i[selBtn=selB]').click(function(){
		    $('input[type=checkbox]').each(function (){                   
		    	if(this.checked){
		    	    this.checked = false;
		    	}else {
		    		this.checked = true;
		    	}
			});         
		});
    }
    
    return {
        init: init
    };
});